package com.manong.bookstore.admin.controller;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.manong.bookstore.admin.entity.ClassifyEntity;
import com.manong.bookstore.admin.service.ClassifyService;
import com.manong.common.utils.PageUtils;
import com.manong.common.utils.R;



/**
 * 
 *
 * @author cyq
 * @email 465447222@qq.com
 * @date 2021-01-26 21:40:49
 */
@RestController
@RequestMapping("admin/classify")
public class ClassifyController {
    @Autowired
    private ClassifyService classifyService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = classifyService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{classifyId}")
    public R info(@PathVariable("classifyId") Integer classifyId){
		ClassifyEntity classify = classifyService.getById(classifyId);

        return R.ok().put("classify", classify);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody ClassifyEntity classify){
		classifyService.save(classify);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody ClassifyEntity classify){
		classifyService.updateById(classify);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Integer[] classifyIds){
		classifyService.removeByIds(Arrays.asList(classifyIds));

        return R.ok();
    }

    /**
     * 提供远程调用 根据classifyId获取分类名字
     * @param classifyId
     * @return
     */
    @ResponseBody
    @RequestMapping("/getName/{classifyId}")
    public String getClassifyName(@PathVariable("classifyId") Integer classifyId){
        ClassifyEntity classify = classifyService.getById(classifyId);
        return classify.getClassifyDesc();
    }

    /**
     * 提供远程调用 获取所有分类名字
     * @param params
     * @return
     */
    @ResponseBody
    @RequestMapping("/listClassifyNames")
    public Object[] listClassifyNames(@RequestParam Map<String, Object> params){
        PageUtils page = classifyService.queryPage(params);
        List<ClassifyEntity> classifyEntities= (List<ClassifyEntity>) page.getList();
        Object[] names=new Object[classifyEntities.size()];
        int i=0;
        for(ClassifyEntity classifyEntity:classifyEntities){
            Map<String, String> name=new HashMap<>();
            name.put("value",classifyEntity.getClassifyId().toString());
            name.put("text",classifyEntity.getClassifyDesc());
            names[i++]=name;
        }
        return names;
    }
}
